package com.ruoyi.web.controller.minglvmini;


import com.github.pagehelper.Page;
import com.minglv.mini.domain.dto.ApplyDto;
import com.minglv.mini.service.IActivityService;
import com.minglv.mini.tools.MiniResult;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "API-活动信息")
@RestController
@RequestMapping("/api/activity")
public class ApiActivityController {
    @Autowired
    private IActivityService activityService;


    @GetMapping("/list")
    @ApiOperation("获取活动列表")
    public MiniResult list(@RequestParam(name = "page") Integer page,
                           @RequestParam(name = "limit",required = false) Integer limit,
                           @RequestParam(name = "active_status") Integer active_status){

        Page<Object> objects = PageUtils.startPage(page, ObjectUtils.isEmpty(limit)?10:limit);
        List<Map> list = activityService.list(active_status);
        Map data = new HashMap();
        data.put("list",list);
        data.put("count",objects.getTotal());
        return  MiniResult.success(data);

    }

    @GetMapping("/info")
    @ApiOperation("获取活动信息")
    public MiniResult info(@RequestParam(name = "id")@ApiParam(value="活动id") Integer id){
        Map activityInfo = activityService.getActivityInfo(id);
        return  MiniResult.success(activityInfo);
    }

    @GetMapping("/iscollect")
    @ApiOperation("活动是否收藏")
    public Object iscollect(HttpServletRequest request, @RequestParam(name = "id")@ApiParam(value="活动id") Integer id){

        String token = request.getHeader("token");
        if (StringUtils.isNotEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX)) {
            token = token.replace(Constants.TOKEN_PREFIX, "");
            Object iscollect = activityService.iscollect(token, id,1);
            if (ObjectUtils.isEmpty(iscollect)) {
                Map reMap = new HashMap();
                reMap.put("code",401);
                reMap.put("msg","Token过期,或无此用户");
                return reMap;
            }else{
                return MiniResult.success(iscollect);
            }
        }else{
            Map reMap = new HashMap();
            reMap.put("code",401);
            reMap.put("msg","Token过期,或无此用户");
            return reMap;
        }
    }

    @PostMapping("/collect")
    @ApiOperation("活动收藏")
    public Object collect(HttpServletRequest request, @RequestBody @ApiParam(value = "活动") Map param) {
        String token = request.getHeader("token");
        if (StringUtils.isNotEmpty(token)) {
            String[] s = token.split(" ");
            if (!ObjectUtils.isEmpty(s) && s.length > 0) {
                token = s[1];
                if (ObjectUtils.isEmpty(param.get("id"))) {
                    return MiniResult.fail();
                }
                Object collect = activityService.collect(token, Long.valueOf(param.get("id").toString()));
                if (ObjectUtils.isEmpty(collect)) {
                    Map reMap = new HashMap();
                    reMap.put("code",401);
                    reMap.put("msg","Token过期,或无此用户");
                    return reMap;
                }else{
                    return MiniResult.success(collect);
                }
            }else{
                Map reMap = new HashMap();
                reMap.put("code",401);
                reMap.put("msg","Token过期,或无此用户");
                return reMap;
            }
        }else{
            Map reMap = new HashMap();
            reMap.put("code",401);
            reMap.put("msg","Token过期,或无此用户");
            return reMap;
        }
    }

    @PostMapping("/apply")
    @ApiOperation("报名活动")
    public Object postApply(HttpServletRequest request, @RequestBody() ApplyDto apply){
        String token = request.getHeader("token");
        if (StringUtils.isNotEmpty(token)) {
            String[] s = token.split(" ");
            if (!ObjectUtils.isEmpty(s) && s.length > 0) {
                token = s[1];
                Object result = activityService.postApply(token,apply);
                return result;
            }else{
                Map reMap = new HashMap();
                reMap.put("code",401);
                reMap.put("msg","Token过期,或无此用户");
                return reMap;
            }
        }else{
            Map reMap = new HashMap();
            reMap.put("code",401);
            reMap.put("msg","Token过期,或无此用户");
            return reMap;
        }
    }

    @GetMapping("/share")
    @ApiOperation("获取分享信息")
    public Object getShare(HttpServletRequest request,@RequestParam(name = "id") Long id) throws IOException {
        String token = request.getHeader("token");
        if (StringUtils.isNotEmpty(token)) {
            String[] s = token.split(" ");
            if (!ObjectUtils.isEmpty(s) && s.length > 0) {
                token = s[1];
                Object result = activityService.share(token,id);
                return result;
            }else{
                Map reMap = new HashMap();
                reMap.put("code",401);
                reMap.put("msg","Token过期,或无此用户");
                return reMap;
            }
        }else{
            Map reMap = new HashMap();
            reMap.put("code",401);
            reMap.put("msg","Token过期,或无此用户");
            return reMap;
        }
    }

}
